package com.hpin.mlxteacher.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.NewFriendsAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.InviteBean;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 新的好友列表
 * <p/>
 * Created by Aaron on 16/2/22.
 */
public class NewFriendsActivity extends BaseActivity {

    private ListView lvnewfriends;
    private List<InviteBean.InvitedListEntity> invitedList = new ArrayList<>();
    private NewFriendsAdapter adapter;
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_new_friends);

        initWidget();

        setAdapter();

        loadNewFriends();

        EventBus.getDefault().register(this);

    }

    private int choosePoint = -1;

    private void setAdapter() {

        adapter = new NewFriendsAdapter(this, invitedList);
        lvnewfriends.setAdapter(adapter);

        lvnewfriends.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (invitedList == null || invitedList.get(position) == null || invitedList.get(position).state != 0)//如果已经处理过好友请求则不能再操作
                    return;

                choosePoint = position;

                mIntent = new Intent(NewFriendsActivity.this, ChatAgreeActivity.class);
                mIntent.putExtra("inviteInfo", invitedList.get(position));
                startActivity(mIntent);
            }
        });

    }

    public void onEventMainThread(Integer style) {

//        LogUtil.e("tag", "== 接收到了 ==");
        switch (style) {

            case -1://处理了好友请求
            case 1:

                if (invitedList == null || choosePoint == -1)
                    return;

                if (invitedList.get(choosePoint) != null) {
                    invitedList.get(choosePoint).state = style;
                    adapter.notifyDataSetChanged();
                }

                break;

        }

    }


    /**
     * 获取好友列表
     */
    private void loadNewFriends() {

        JSONObject j = new JSONObject();

        j.put("token", sp.getString(Constant.token, ""));

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT_TALK + Constant.INVITEDLIST,
                j.toString(), getClass().getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            InviteBean ib = JSONObject.parseObject(str, InviteBean.class);
                            if (ib.code == Constant.SUCCESS) {

                                if (ib.invitedList == null)
                                    return;

                                invitedList = ib.invitedList;
                                adapter.setInvitedList(invitedList);
                                adapter.notifyDataSetChanged();

                            } else {
                                showToast(Constant.showErrorMsg(ib.code, NewFriendsActivity.this));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null);
    }

    private void initWidget() {

        this.lvnewfriends = (ListView) findViewById(R.id.lv_new_friends);
        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.new_friends));

        iv_left.setOnClickListener(this);
    }

//    public void handleFriendRequest(String phoneNum, final NewFriendsAdapter.AddSuccessListener addSuccessListener) {
//
//        JSONObject j = new JSONObject();
//        j.put("token", sp.getString(Constant.TOKEN, ""));
//        j.put("isAgree", 1);
//        j.put("phoneNum", phoneNum);
//
//        HttpUtil.setPostRequest("处理中...", this, Constant.WEB_ROOT_TALK + Constant.DEALINVITE, j.toJSONString(),
//                getClass().getName(), new HttpUtil.SuccessListener() {
//                    @Override
//                    public void setOnSuccessListener(String str) {
//
//                        try {
//
//                            org.json.JSONObject result = new org.json.JSONObject(str);
//
//                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {
//
//
//                               if (addSuccessListener!=null)
//                                   addSuccessListener.onSuccessListener();
//
//                            } else {
//                                showToast(Constant.showErrorMsg(result.getInt("code"), NewFriendsActivity.this));
//                            }
//
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//
//                    }
//                }, null);
//
//    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());

        EventBus.getDefault().unregister(this);
    }
}
